package com.thinkgsp.webapp.controller.rp;

import java.sql.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgsp.jxc.service.RpBillCheckService;
import com.thinkgsp.jxc.service.RpBillService;
import com.thinkgsp.webapp.util.ExtReturn;

/**
 * 应付款核销控制器
 * @author evan
 *
 */
@Controller
@RequestMapping(value="/rp/apaycheck")
public class APayCheckController {
	@Autowired
	private RpBillCheckService billCheckService;
	@Autowired
	private RpBillService rpBillService;
	
	@RequestMapping(value="/getCheckLogByDate.do")
	@ResponseBody
	public Object getCheckLog(@RequestParam("begDate") Date begDate,
								@RequestParam("endDate") Date endDate,
								@RequestParam("custId") Integer custId,
								@RequestParam("start") int start,
								@RequestParam("limit") int limit) {
		if(custId != null && custId.intValue() == 0) {
			custId = null;
		}
		return billCheckService.findCheckLog(-1, begDate, endDate, custId,start,limit);
	}
	@RequestMapping(value="/getCheckLogByPeriod.do")
	@ResponseBody
	public Object getCheckLog(@RequestParam("begYear") Integer begYear,
								@RequestParam("begPeriod") Integer begPeriod,
								@RequestParam("endYear") Integer endYear,
								@RequestParam("endPeriod") Integer endPeriod,
								@RequestParam("custId") Integer custId,
								@RequestParam("start") int start,
								@RequestParam("limit") int limit) {
		if(custId != null && custId.intValue() == 0) {
			custId = null;
		}
		return billCheckService.findCheckLog(-1, begYear, begPeriod, endYear, endPeriod, custId,start,limit);
	}
	
	@RequestMapping(value="/getUnSettleARecBill.do")
	@ResponseBody
	public Object getUnSettleARecBills(@RequestParam("custId")Integer custId) {
		return rpBillService.getUnSettleARecBill(custId);
	}
	
	@RequestMapping(value="/getUnSettleAPayBill.do")
	@ResponseBody
	public Object getUnSettleAPayBills(@RequestParam("custId")Integer custId) {
		return rpBillService.getUnSettleAPayBill(custId);
	}
	
	@RequestMapping(value="/getUnSettlePayBill.do")
	@ResponseBody
	public Object getUnSettlePayBills(@RequestParam("custId")Integer custId) {
		return rpBillService.getUnSettlePayBill(custId);
	}
	
	@RequestMapping(value="/getUnSettleRefundPayBill.do")
	@ResponseBody
	public Object getUnSettleRefundPayBills(@RequestParam("custId")Integer custId) {
		return rpBillService.getUnSettleRefundPayBill(custId);
	}

	@RequestMapping(value="/checkForPayBill.do")
	@ResponseBody
	public Object checkForRecBill(@RequestBody CheckForPayBillParam params) {
		if(params == null) {
			return new ExtReturn("参数无效:params");
		}
		if(params.getPayBills() == null || params.getPayBills().size() == 0) {
			return new ExtReturn("参数无效:payBills");
		}
		if(params.getApayBills() == null || params.getApayBills().size() == 0) {
			return new ExtReturn("参数无效:apayBills");
		}
		try {
			billCheckService.checkForPayBill(params.getPayBills(), params.getApayBills());
			return new ExtReturn(true,"核销成功!");
		} catch(Exception e) {
			return new ExtReturn(e.getMessage());
		}
	}
	
	@RequestMapping(value="/checkForRefundPayBill.do")
	@ResponseBody
	public Object checkForRefundRecBill(@RequestBody CheckForRefundPayBillParam params) {
		if(params == null) {
			return new ExtReturn("参数无效:params");
		}
		if(params.getPayBills() == null || params.getPayBills().size() == 0) {
			return new ExtReturn("参数无效:payBills");
		}
		if(params.getRefundPayBills() == null || params.getRefundPayBills().size() == 0) {
			return new ExtReturn("参数无效:refundPayBills");
		}
		try {
			billCheckService.checkForRefundPayBill(params.getRefundPayBills(), params.getPayBills());
			return new ExtReturn(true,"核销成功!");
		} catch(Exception e) {
			return new ExtReturn(e.getMessage());
		}
	}
	
	@RequestMapping(value="/checkForApayBillOffsetArecBill.do")
	@ResponseBody
	public Object checkForApayBillOffsetArecBill(@RequestBody CheckForArecBillOffsetApayBillParam params) {
		if(params == null) {
			return new ExtReturn("参数无效:params");
		}
		if(params.getApayBills() == null || params.getApayBills().size() == 0) {
			return new ExtReturn("参数无效:ApayBills");
		}
		if(params.getArecBills() == null || params.getArecBills().size() == 0) {
			return new ExtReturn("参数无效:ArecBills");
		}
		try {
			billCheckService.checkForApayBillOffsetArecBill(params.getApayBills(), params.getArecBills());
			return new ExtReturn(true,"核销成功!");
		} catch(Exception e) {
			return new ExtReturn(e.getMessage());
		}
	}
	
	@RequestMapping(value="/checkForApayBillTransfer.do")
	@ResponseBody
	public Object checkForArecBillTransfer(@RequestBody CheckForApayBillTransferParam params) {
		if(params == null) {
			return new ExtReturn("参数无效:params");
		}
		if(params.getApayBill() == null || params.getApayBill().getArpBillId().longValue() == 0L) {
			return new ExtReturn("参数无效:APayBill");
		}
		if(params.getCustSuppId() == null || params.getCustSuppId().intValue() == 0) {
			return new ExtReturn("参数无效:custSuppId");
		}
		try {
			billCheckService.checkForApayBillTransfer(params.getApayBill(), params.getCustSuppId());
			return new ExtReturn(true,"核销成功!");
		} catch(Exception e) {
			return new ExtReturn(e.getMessage());
		}
	}
	
	@RequestMapping(value="/checkForRevoke.do")
	@ResponseBody
	public Object checkForRevoke(@RequestParam("checkId") Long checkId) {
		if(checkId == null || checkId.longValue() == 0L) {
			return new ExtReturn("参数无效:checkId");
		}
		try {
			billCheckService.checkForRevoke(checkId);
			return new ExtReturn(true,"核销成功!");
		} catch(Exception e) {
			return new ExtReturn(e.getMessage());
		}
	}
}
